#!/bin/sh
# This shell script generates data.h, a C header file containing fixed data
# for use in TubeMan. It includes an array of common user agent strings, and
# the default configuration. Running this script requires curl, grep and sed.

printf " Generating data.h... "
url="https://techblog.willshouse.com/2012/01/03/most-common-user-agents/"
list=$(curl -s "$url" | grep "Mozilla/" | sed -e "s/.*>//g" -e "/^$/d")
html=$(curl -sA "$(echo $list | head -n1)" "https://www.youtube.com/")
clientname=$(echo "$html" | grep -Eo "CLIENT_NAME\":[^,}]+[,}]" | \
             sed -e 's/CLIENT_.*\"://g' -e 's/,//g')
clientver=$(echo "$html" | grep -Eo "CLIENT_VERSION\":\"[^\"]+\"" | \
            sed -e 's/CLIENT_.*\":\"//g' -e 's/\"//g')
#shellcheck disable=SC2016
prog=$(autoconf -t 'AC_INIT:$1')
cat <<EOF > src/data.h
/*
  Copyright $(date +%Y) Oliver Galvin <odg@riseup.net>
  This file is part of $prog.

 $prog is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 $prog is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with $prog.  If not, see <http://www.gnu.org/licenses/>.

 This file was generated by $0 on $(date +%F).
 */

#define MAXUANUM $(echo "$list" | wc -l)
#define YTCLIENTNAME "${clientname}"
#define YTCLIENTVER "${clientver}"

const char *useragents[] = {
$(echo "$list" | sed -e 's/^/\t\"/' -e 's/$/\",/')
	""
};

const char *defaultconfig[] = {
$(sed -e 's/^/\t\"/g' -e 's/$/\\n\",/g' docs/config)
	""
};
EOF
echo "done."
